﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Admin_UpdateXz : System.Web.UI.Page
{
    AccessDAO dao = new AccessDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsAdmin.Admin();
        this.btnSubmit.Attributes.Add("onclick", "javascript:showmessagebox();");
        if (!Page.IsPostBack)
        {
            int typeid = Convert.ToInt32(Session["xzId"].ToString());
            string sql = "select * from xzInfo where typeid=" + typeid;
            DataSet ds = dao.getDs(sql);
            foreach (DataRow dr in ds.Tables["message"].Rows)
            {
                this.drpType.Items.Add(dr["typeName"].ToString());
            }

            int id = Convert.ToInt32(Session["xzMessId"].ToString());
            string sql1 = "select * from xzMessage where id=" + id;
            DataSet ds1 = dao.getDs(sql1);
            foreach (DataRow dr in ds1.Tables["message"].Rows)
            {
                this.txtTitle.Text = dr["name"].ToString();
                this.txtDx.Text = dr["xzdx"].ToString();
                this.txtLanguage.Text = dr["xzlanguage"].ToString();
                this.txtHj.Text = dr["xzhj"].ToString();
                this.DateTimePicker1.Text = dr["xzTime"].ToString();
                this.FCKeditor1.Value = dr["content"].ToString();
                Session["file"] = dr["xzfile"].ToString();

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Session["xzMessId"].ToString());
        string name = this.txtTitle.Text;
        string dx = this.txtDx.Text.ToString();
        string language = this.txtLanguage.Text;
        string hj = this.txtHj.Text;
        string file = this.FileUpload1.PostedFile.FileName;
        string fileName = "File/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();


        DateTime tt = Convert.ToDateTime(this.DateTimePicker1.Text.ToString());
        string time = tt.ToShortDateString();

        string content = this.FCKeditor1.Value;

        if (!file.Equals(""))
        {
            if (!Path.GetExtension(file).Equals(".txt"))
            {
            fileName += Path.GetExtension(file);
            string sql = "update xzMessage set name='" + name + "',xzlanguage='" + language + "',xzdx='" + dx + "',xzhj='" + hj +"',xzfile='"+fileName+ "',xzTime='" + time + "',content='" + content + "' where id=" + id;
            if (dao.getComm(sql) != 0)
            {
                this.FileUpload1.PostedFile.SaveAs(Server.MapPath("~/") + "\\" + fileName);

                Server.Transfer("xzMessage1.aspx");
            }
            else
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('修改失败');</script>");
            }
        }
        else
        {
            RegisterStartupScript("check", "<script language='javascript'>alert('不支持.txt文件');</script>");
        }
        }
        else
        {
            string sql1 = "update xzMessage set name='" + name + "',xzlanguage='" + language + "',xzdx='" + dx + "',xzhj='" + hj + "',xzfile='" + Session["file"].ToString() + "',xzTime='" + time + "',content='" + content + "' where id=" + id;

            if (dao.getComm(sql1) != 0)
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('更新成功');</script>");

                Server.Transfer("xzMessage1.aspx");
            }
            else
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('更新失败');</script>");
            }

        }
    }
    //取消
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("xzMessage1.aspx");
    }
}
